import { defineStore } from 'pinia';
import { NoCacheState } from '@/store/interface';

// 检测是否H5
const isH5Platform = () => {
  // 检测用户代理字符串
  const userAgentRegex = /Mobile|Android|iPhone|iPod|iPad|Windows Phone/i;
  const isUserAgentH5 = userAgentRegex.test(navigator.userAgent);
  // 检测设备方向属性
  const isOrientationH5 = typeof window.orientation !== 'undefined';
  // 检测触摸事件支持
  const isTouchH5 = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || (navigator as any).msMaxTouchPoints > 0;
  // 综合判断
  return isUserAgentH5 || isOrientationH5 || isTouchH5;
}

export const nocache = defineStore({
  id: 'nocache',
  state: (): NoCacheState => ({
    isH5: isH5Platform(),
  }),
  persist: false,
  getters: {
    getIsH5: (state) => state.isH5,
  },
  actions: {
  },
});

export default nocache;
